package com.example.piece.tools

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.graphics.Path
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi

object ClickUtils {
    /**
     *StrokeDescription 的后两个参数表示 startTime 和 duration，分别表示手势的开始时间以及持续时间，以毫秒为单位。我将其设置为 0 和 1，也就是 1ms 以内完成从 (x, y) 坐标移动到 (x, y) 坐标。
     *X 300 -310 y  1000 - 500
     * startTime 0
     * duration  2000
     */
    @RequiresApi(Build.VERSION_CODES.N)
    fun click(accessibilityService: AccessibilityService, xStart: Float,xTo: Float, yStart: Float,yTo: Float,startTime: Long=0,duration: Long=1000) {
        //Log.d("~~~", "click: ($x, $y)")
        val builder = GestureDescription.Builder()
        val path = Path()
        path.moveTo(xStart, yStart)
        path.lineTo(xTo, yTo)
        builder.addStroke(GestureDescription.StrokeDescription(path, startTime, duration))
        val gesture = builder.build()
        accessibilityService.dispatchGesture(gesture, object : AccessibilityService.GestureResultCallback() {
            override fun onCancelled(gestureDescription: GestureDescription) {
                super.onCancelled(gestureDescription)
            }

            override fun onCompleted(gestureDescription: GestureDescription) {
                super.onCompleted(gestureDescription)
            }
        }, null)
    }
}